<template>
  <div>
    <div class="header_search_nav search_form">
      <div class="ipt_left">
        <el-form ref="queryForm" :model="queryParams" label-width="80px">
          <el-row>
            <el-col :span="6">
              <div class="timebox">
                <el-form-item label="名称" prop="categoryName">
                  <el-input
                    v-model="queryParams.categoryName"
                    class="w-200"
                    placeholder="请输入名称"
                  />
                </el-form-item>
              </div>
            </el-col>
            <el-col :span="6">
              <el-form-item label="类型" prop="categoryType">
                <el-select
                  v-model="queryParams.categoryType"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in categoryTypeList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>

      <div class="btn_right">
        <button class="btn1" @click="handleQuery">筛选</button>
        <button class="btn2" @click="resetQuery">重置</button>
      </div>
    </div>
    <div class="table_card">
      <div class="table_card_top">
        <span class="solid_left"></span>
        <div class="table_card_title">收支类目</div>
        <div class="op_right">
          <button class="primary_button" @click="NewUser">新增类目</button>
        </div>
      </div>

      <div class="table_list">
        <el-table :data="categoryList" stripe style="width: 100%">
          <el-table-column prop="categorySeq" label="类目排序">
          </el-table-column>
          <el-table-column prop="categoryTypeStr" label="类目类型">
          </el-table-column>
          <el-table-column prop="categoryPName" label="父类目" width="150px">
          </el-table-column>
          <el-table-column prop="categoryCode" label="类目编码">
          </el-table-column>
          <el-table-column prop="categoryName" label="类目名称">
          </el-table-column>
          <el-table-column prop="categoryDesc" label="类目备注">
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button size="mini" type="text" @click="edit(scope.row)"
                >编辑</el-button
              >

              <el-button size="mini" type="text" @click="DelUser(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <pagination
          :total="total"
          :page.sync="queryParams.pageNo"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>

      <!-- 新建收支 -->
      <el-dialog
        :visible.sync="categoryVisible"
        width="500px"
        :title="title"
        class="no-dialog-padding"
        @close="Canceluser"
      >
        <span>
          <div class="source_box">
            <el-form
              :model="CategoryForm"
              :rules="CategoryRules"
              ref="CategoryForm"
              label-width="100px"
              class="demo-ruleForm"
            >
              <el-form-item label="排序" prop="categorySeq">
                <el-input-number
                  v-model="CategoryForm.categorySeq"
                  :min="0"
                  :max="10000"
                  :controls="false"
                  class="w-100 text-left"
                ></el-input-number>
              </el-form-item>
              <el-form-item label="类目类型" prop="categoryType">
                <el-select
                  v-model="CategoryForm.categoryType"
                  placeholder="请选择"
                  class="w-100"
                  @change="FormchangeCategoryType"
                >
                  <el-option
                    v-for="item in categoryTypeList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>

              <el-form-item label="父类目" prop="categoryPid">
                <el-select
                  v-model="CategoryForm.categoryPid"
                  placeholder="请选择"
                  class="w-100"
                >
                  <el-option
                    v-for="item in FormcategoryArry"
                    :key="item.categoryId"
                    :label="item.categoryName"
                    :value="item.categoryId"
                  >
                  </el-option>
                </el-select>
              </el-form-item>

              <el-form-item label="类目编码" prop="categoryCode">
                <el-input v-model="CategoryForm.categoryCode"></el-input>
              </el-form-item>
              <el-form-item label="名称" prop="categoryName">
                <el-input v-model="CategoryForm.categoryName"></el-input>
              </el-form-item>
              <el-form-item label="备注" prop="categoryDesc">
                <el-input v-model="CategoryForm.categoryDesc"></el-input>
              </el-form-item>
            </el-form>
          </div>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button class="cancel_btn" @click="Canceluser">取 消</el-button>
          <el-button
            type="primary"
            @click="userSubmit"
            :loading="CreateClienLoading"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  getCategoryPage,
  CreateCategory,
  UpdateCategory,
  getCategory,
  DeleteCategory,
} from "@/api/bpm/category";
import { checkEnglish } from "@/utils/regular";
import { getAllCategory } from "@/api/bpm/category";

export default {
  name: "EryuoaStoreCategoryList",
  data() {
    return {
      CategoryForm: {
        categoryName: "",
        categoryType: "",
        categorySeq: "",
        categoryDesc: "",
        categoryCode: "",
        categoryId: "",
        categoryPid: "",
      },
      CategoryRules: {
        categoryName: [
          { required: true, message: "请输入类目名称", trigger: "change" },
        ],
        categoryType: [
          { required: true, message: "请选择类目类型", trigger: "change" },
        ],
        // categoryPid: [
        //   { required: true, message: "请选择类目", trigger: "change" },
        // ],
        categorySeq: [
          { required: true, message: "请输入排序", trigger: "change" },
        ],
        categoryCode: [
          { required: true, message: "请输入类目编码" },
          {
            validator: this.CheckEnglish,
            trigger: "blur",
          },
        ],
      },
      categoryVisible: false, // 新增收支类目
      categoryList: [], // 收支类目数据
      loading: false,
      title: "",
      total: 0,
      CreateClienLoading: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
      },
      // 查询参数
      queryParam: {
        pageNo: 1,
        pageSize: 10,
      },

      categoryTypeList: [
        {
          value: "D",
          label: "扣款",
        },
        {
          value: "P",
          label: "支出",
        },
        {
          value: "I",
          label: "收入",
        },
        {
          value: "U",
          label: "上月未收",
        },
        {
          value: "T",
          label: "本月未收",
        },
        {
          value: "C",
          label: "手续费",
        },
      ],
      FormcategoryArry: [],
    };
  },

  created() {
    this.getList();
  },

  mounted() {},

  methods: {
    FormchangeCategoryType() {
      this.getAllCategoryList();
      this.CategoryForm.categoryPid = "";
    },

    CheckEnglish(rule, value, callback) {
      if (!checkEnglish(value) && !!value) {
        callback(new Error("只能输入英文字母"));
      } else {
        callback();
      }
    },
    // 获取类目详情
    getCategorys(categoryId) {
      getCategory({
        categoryId: categoryId,
      })
        .then((res) => {
          const { code, data } = res;
          if (code === 0) {
            this.CategoryForm.categoryName = data.categoryName;
            this.CategoryForm.categorySeq = data.categorySeq;
            this.CategoryForm.categoryType = data.categoryType;
            this.CategoryForm.categoryDesc = data.categoryDesc;
            this.CategoryForm.categoryCode = data.categoryCode;
          }
          this.getAllCategoryList();
        })
        .catch((err) => {});
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParam.pageNo = 1;
      this.getLists();
    },

    // 收支类目列表
    getList() {
      this.loading = true;
      getCategoryPage(this.queryParams)
        .then((res) => {
          this.loading = false;
          const { code, data } = res;
          if (code === 0) {
            this.categoryList = data.list;
            this.total = data.total;
          }
        })
        .catch((err) => {
          this.loading = false;
        });
    },

    Canceluser() {
      this.resetFormData("CategoryForm");
      this.CreateClienLoading = false;
      this.$nextTick(() => {
        this.categoryVisible = false;
      });
    },

    // 筛选
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetFormData("queryForm");
      // this.queryParams.localDate = dayDataTime("obj").str;
      this.handleQuery();
    },

    // 新增类目
    userSubmit() {
      this.$refs["CategoryForm"].validate((valid) => {
        console.log(this.CategoryForm);
        if (valid) {
          if (!!this.CategoryForm.categoryId) {
            this.UpdateCategorys();
          } else {
            this.CreateCategorys();
          }
        } else {
          return false;
        }
      });
    },

    // 新增
    CreateCategorys() {
      CreateCategory(this.CategoryForm)
        .then((res) => {
          const { code } = res;
          if (code === 0) {
            this.Canceluser();
            this.$message.success("操作成功");
            this.getList();
          }
        })
        .catch((err) => {});
    },

    // 编辑
    UpdateCategorys() {
      let CategoryForm = JSON.parse(JSON.stringify(this.CategoryForm));
      UpdateCategory(CategoryForm)
        .then((res) => {
          const { code } = res;
          if (code === 0) {
            this.Canceluser();
            this.$message.success("操作成功");
            this.getList();
          }
        })
        .catch((err) => {});
    },

    // 全部类目
    getAllCategoryList() {
      getAllCategory({
        categoryType: this.CategoryForm.categoryType,
      })
        .then((res) => {
          const { code, data } = res;
          if (code === 0) {
            this.FormcategoryArry = data;
          }
        })
        .catch((err) => {});
    },

    // 新建
    NewUser() {
      this.categoryVisible = true;
      this.title = "新增类目";
      this.CategoryForm.categoryId = "";
      this.CategoryForm.categoryPid = "";
      this.CategoryForm.categoryName = "";
    },

    // 编辑类目
    edit(row) {
      this.categoryVisible = true;
      this.title = "编辑类目";
      this.CategoryForm.categoryId = row.categoryId;
      this.CategoryForm.categoryPid = row.categoryPid;
      this.CategoryForm.categoryName = row.categoryName;
      this.getCategorys(row.categoryId);
    },

    // 表单重置
    resetFormData(formName) {
      this.$refs[formName].resetFields();
    },

    // 删除用户
    DelUser(row) {
      this.$confirm("类目删除后将无法撤回, 是否删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          DeleteCategory({ categoryId: row.categoryId })
            .then((res) => {
              const { code } = res;
              if (code === 0) {
                this.getList();
                this.$modal.msgSuccess("操作成功");
              }
            })
            .catch(() => {});
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="scss" scoped>
.w-200 {
  width: 200px;
  background: #ffffff;
  border-radius: 4px;
}

.client_name {
  font-size: 14px;
  font-weight: 400;
  color: #0052d9;
}

.give {
  height: 40px;
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
  margin-top: 40px;
  line-height: 40px;
}

.el-form-item {
  margin-bottom: 20px;
}

.recharge_form {
  padding: 10px 24px;
}

.memberName {
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
}
.el_row_item {
  margin-bottom: 10px;
}
.el_row_date {
  margin-top: 20px;
}
.el_row_reason {
  margin-top: 10px;
}

.el_row_date {
  .el-form-item {
    margin-bottom: 0px;
  }
}
.source_box {
  margin: 10px 24px;
}
</style>
